package com.myfoon.servlet.accounts;

import com.myfoon.action.backing.system.UserBean;
import com.myfoon.helper.RedirectHelper;
import com.myfoon.helper.SessionHelper;
import com.myfoon.model.ejb.UserEJBLocal;
import com.myfoon.model.entity.User;

import com.myfoon.model.exception.PasswordNotMatchException;

import java.io.IOException;

import javax.ejb.EJB;

import javax.ejb.EJBException;

import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Login extends HttpServlet {
    @EJB 
    private UserEJBLocal userLocal;
    
    public Login() {
    }
    
    @Override
    protected void doPost(HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) throws ServletException,
                                                                          IOException {        
        String email = httpServletRequest.getParameter("email");
        String password = httpServletRequest.getParameter("password");                        
        String client = httpServletRequest.getParameter("client");
        String tz_offset = httpServletRequest.getParameter("tz_offset");
                        
        if ( email == null || password == null || email.equals("") || password.equals("") ) {
            RedirectHelper.redirectToError(httpServletResponse, 1);
            return;
        }
                 
        try {            
            User user;                                     
            user = userLocal.findByEmailAndPassword(email, password); 
            httpServletRequest.getSession(true).setAttribute("user", user);            
            
            Cookie cookie = new Cookie("tz_offset", tz_offset);
            cookie.setMaxAge(7 * 24 * 60 * 60);         
            cookie.setPath("/");
            httpServletResponse.addCookie(cookie);    
            httpServletRequest.getSession(true).setAttribute("tz_offset", tz_offset);
            httpServletResponse.sendRedirect("/home.jsp");            
            return;
        }
        catch(EJBException e) {
            if (e.getCausedByException() instanceof PasswordNotMatchException) {
                httpServletResponse.sendRedirect("/error#1");                
                return;
            }
            if (e.getCausedByException() instanceof EntityNotFoundException) {
                httpServletResponse.sendRedirect("/error#2");
                return;    
            }
            else 
            if (e.getCausedByException() instanceof NoResultException) {
                httpServletResponse.sendRedirect("/error#3");
                return;
            }                
        }        
    }

    @Override
    protected void doGet(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse) throws ServletException,
                                                                         IOException {
        httpServletResponse.sendRedirect("/login.jsp");
    }
}
